package com.lanou.liuqixin.datebasedemo.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 17/4/20.
 */

public class LinearChartView extends View {
    private String[] mNames;
    private int mTvSizes;
    private int mNums;
    private List<Point> points;
    private int[] total;

    public void setTotal(int[] total) {
        this.total = total;
    }

    public void setNames(String[] names) {
        mNames = names;
    }

    public void setTvSizes(int tvSizes) {
        mTvSizes = tvSizes;
    }

    public void setNums(int nums) {
        mNums = nums;
    }
    public void MyNotify(){
        LinearChartView.this.invalidate();
    }

    private static final int PANDING_WIDTH = 150;
    private static final int PANDING_HEIGHT = 400;
    private static final int COUNT_Y_MARK = 16;
    private int[] values = new int[20];
    private int mWidth;
    private int mHeight;
    private Paint mPaint;

    public LinearChartView(Context context) {
        super(context);
        init();
    }

    public LinearChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        points =new ArrayList<>();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        values[0] = 15;
        values[1] = 14;
        values[2] = 13;
        values[3] = 12;
        values[4] = 11;
        values[5] = 10;
        values[6] = 9;
        values[7] = 8;
        values[8] = 7;
        values[9] = 6;
        values[10] = 5;
        values[11] = 4;
        values[12] = 3;
        values[13] = 2;
        values[14] = 1;
        values[15] = 0;


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawAxisX(canvas);
        drawAxisY(canvas);
        drawMarkX(canvas);
        drawMarkY(canvas);
        drawPoint(canvas);
        drawLine(canvas);

    }

    private void drawMarkX(Canvas canvas) {
        float length = (mWidth - PANDING_WIDTH * 2) / (mNums + 1);
        float mX = mWidth - PANDING_WIDTH + 20;
        float mY = mHeight - PANDING_HEIGHT + 270;
        mPaint.setTextSize(30);
        canvas.drawText("月份", mX, mY, mPaint);

        for (int i = 0; i < mNums; i++) {
            float startX = PANDING_WIDTH + length * (i + 1);
            float startY = mHeight - PANDING_HEIGHT + 240;
            float stopX = startX;
            float stopY = mHeight - PANDING_HEIGHT + 260;
            canvas.drawLine(startX, startY, stopX, stopY, mPaint);
            mPaint.setTextSize(30);

        }
        for (int i = 0; i < mNames.length; i++) {
            float lengthMark = (mWidth - PANDING_WIDTH * 2) / (mNames.length + 1);
            float x = PANDING_WIDTH + lengthMark * (i + 1);
            float y = mHeight - PANDING_HEIGHT + 300;
            mPaint.setTextSize(35);
            canvas.drawText(String.valueOf(mNames[i]), x, y, mPaint);

        }


    }

    private void drawMarkY(Canvas canvas) {
        float length = (mHeight - 2 * PANDING_HEIGHT + 400) / (COUNT_Y_MARK + 1);
        float mX = PANDING_WIDTH - 40;
        float mY = PANDING_HEIGHT - 350;
        mPaint.setTextSize(30);
        canvas.drawText("油耗", mX, mY, mPaint);
        for (int i = 0; i < COUNT_Y_MARK; i++) {
            float startX = PANDING_WIDTH;
            float startY = PANDING_HEIGHT + length * (i + 1) - 130;
            float stopX = PANDING_WIDTH + 25;
            float stopY = startY;
            float x = PANDING_WIDTH - 40;
            float y = PANDING_HEIGHT + length * (i + 1) - 90;
            mPaint.setTextSize(30);
            canvas.drawLine(startX, startY, stopX, stopY, mPaint);
            canvas.drawText(String.valueOf(values[i]), x, y, mPaint);

        }

    }

    private void drawAxisX(Canvas canvas) {
        float startX = PANDING_WIDTH;
        float startY = PANDING_HEIGHT + 360;
        float stopX = mWidth - PANDING_WIDTH;
        float stopY = startY;
        canvas.drawLine(startX, startY, stopX, stopY, mPaint);
    }

    private void drawAxisY(Canvas canvas) {
        float startY = PANDING_HEIGHT - 300;
        float stopY = mHeight - 140;
        float startX = PANDING_WIDTH;
        float stopX = startX;
        canvas.drawLine(startX, startY, stopX, stopY, mPaint);

    }
    private void drawLine(Canvas canvas){
        Point lastPoint = null ;
        for (int i = 0; i < points.size(); i++) {
            Point current =points.get(i);
            if (i != 0){
                canvas.drawLine(lastPoint.x,lastPoint.y,current.x,current.y,mPaint);
            }
            lastPoint = current;
        }
            points.clear();
    }
    private void drawPoint(Canvas canvas){
        float length = (mWidth - PANDING_WIDTH * 2) / (mNums + 1);
        float heigh =mHeight - PANDING_HEIGHT * 2 ;
        for (int i = 0; i < mNums; i++) {
            float cx = PANDING_WIDTH + length * ( i + 1 );
            float cy = (30 - total[i]) * heigh / 40  +PANDING_HEIGHT;
            float radius = 5 ;
            canvas.drawCircle(cx,cy,radius,mPaint);
            points.add(new Point(cx,cy));


        }


    }




    //自己定义的point
    private static class Point{
        public final float x;
        public final float y;
        public Point(float x , float y){
            this.x = x;
            this.y = y;
        }


    }




}
